﻿#include "stdafx.h"
#include "POSCheck.h"
#include "DlgPrinterCheck.h"
#include "Printer.h"
#include "DlgPrinterAdvCheck.h"

//IMPLEMENT_DYNAMIC(CDlgPrinterCheck, CDialog)

CDlgPrinterCheck::CDlgPrinterCheck(CWnd* pParent /*=NULL*/)
	: CDialog(CDlgPrinterCheck::IDD, pParent)
{
    m_pPrinter = NULL;
    m_nStatus = ES_Unchecked;
    m_bPT = FALSE;
}

CDlgPrinterCheck::~CDlgPrinterCheck()
{
    delete m_pPrinter;
}

void CDlgPrinterCheck::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
}

void CDlgPrinterCheck::SetPrinterInfo(int nType, int nPort)
{
    switch(nType)
    {
    case T90:
        m_pPrinter = new CPT900(nPort);
        break;

    case POS88VI:
        m_pPrinter = new CPOS88VI(nPort);
        break;

    case PR58:
        m_pPrinter = new CPR58(nPort);
        break;

    default:
        break;
    }
}

BEGIN_MESSAGE_MAP(CDlgPrinterCheck, CDialog)
    ON_BN_CLICKED(IDOK, &CDlgPrinterCheck::OnBnClickedOk)
    ON_BN_CLICKED(IDC_BTN_PRT_ADVTEST, &CDlgPrinterCheck::OnBnClickedBtnPrtAdvtest)
    ON_BN_CLICKED(IDC_RADIO_PT_NORMAL, &CDlgPrinterCheck::OnBnClickedRadioNormal)
    ON_BN_CLICKED(IDC_RADIO_PT_ERROR, &CDlgPrinterCheck::OnBnClickedRadioError)
    ON_BN_CLICKED(IDC_RADIO_PT_PASS, &CDlgPrinterCheck::OnBnClickedRadioPass)
END_MESSAGE_MAP()

BOOL CDlgPrinterCheck::OnInitDialog()
{
    CDialog::OnInitDialog();
    GetDlgItem(IDC_EDIT_PRINTER_INPUT)->SetFocus();
    
    GetDlgItem(IDC_RADIO_PT_NORMAL)->EnableWindow(m_bPT);
    GetDlgItem(IDC_RADIO_PT_ERROR)->EnableWindow(m_bPT);
    GetDlgItem(IDC_RADIO_PT_PASS)->EnableWindow(m_bPT);
    GetDlgItem(IDOK)->EnableWindow(!m_bPT);

    if (NULL == m_pPrinter) return FALSE;
    
    return m_pPrinter->Initialize();
}

BOOL CDlgPrinterCheck::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN)
    {
        return CDialog::PreTranslateMessage(pMsg);
    }

    if(pMsg->message == WM_CHAR)
    {
        char szCharacter[50] = "";
        memset(szCharacter, pMsg->wParam, 50);
        CString strInput(szCharacter);
        SetDlgItemText(IDC_EDIT_PRINTER_INPUT, strInput);
        
        if (NULL == m_pPrinter)
            return CDialog::PreTranslateMessage(pMsg);

        switch(m_pPrinter->GetType())
        {
        case T90:
        case POS88VI:
        case PR58:
            m_pPrinter->Print((LPCTSTR)szCharacter, 50);
            break;

        default:
            break;
        }
    }
   
    return CDialog::PreTranslateMessage(pMsg);
}

void CDlgPrinterCheck::OnBnClickedOk()
{
    if (NULL != m_pPrinter)
    {
        m_pPrinter->Close();
    }

    GetParent()->PostMessage(WM_PT_PRINTER, m_nStatus);

    OnOK();
}


void CDlgPrinterCheck::OnBnClickedBtnPrtAdvtest()
{
    CDlgPrinterAdvCheck dlg;

    dlg.RegistPrinter(m_pPrinter);
    dlg.DoModal();

}


void CDlgPrinterCheck::OnBnClickedRadioNormal()
{
    m_nStatus = ES_Normal;
    GetDlgItem(IDOK)->EnableWindow();
}


void CDlgPrinterCheck::OnBnClickedRadioError()
{
    m_nStatus = ES_Exception;
    GetDlgItem(IDOK)->EnableWindow();
}


void CDlgPrinterCheck::OnBnClickedRadioPass()
{
    m_nStatus = ES_Pass;
    GetDlgItem(IDOK)->EnableWindow();
}
